package com.opengw.configuration.entity;

import java.io.ByteArrayInputStream;
import java.io.Serializable;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.opengw.configuration.CertificateManager;
import com.opengw.configuration.support.Base64;
import com.opengw.configuration.support.Constants;

@Entity
@Table(name = "certificate_info")
public class CertificateInfo implements Serializable{
	
	private static final long serialVersionUID = -2110747623545983958L;
	private long certificateInfoId;
	private String certificateDecoded;
	private X509Certificate certificate;
	private String issuedTo;
	private String issuedBy;
	private String notBefore;
	private String notAfter;
	
	public CertificateInfo() {
	}
	
	public CertificateInfo(X509Certificate certificate){
		this.certificate = certificate;
		CertificateManager certificateManager = CertificateManager.getCertificateManager();
		this.issuedTo = certificateManager.getCertificateSubjectDNCN(certificate);
		this.issuedBy = certificateManager.getCertificateIssuerDNCN(certificate);
		SimpleDateFormat formatter = new SimpleDateFormat(Constants.SHORT_DATE_FORMAT);		
		this.notBefore =  formatter.format(certificate.getNotBefore());
		this.notAfter =  formatter.format(certificate.getNotAfter());
	}

	public void setCertificate(X509Certificate certificate) {
		this.certificate = certificate;
	}

	@Transient
	public X509Certificate getCertificate() {
		return certificate;
	}

	public void setNotBefore(String notBefore) {
		this.notBefore = notBefore;
	}

	@Transient
	public String getNotBefore() {
		return notBefore;
	}

	public void setNotAfter(String notAfter) {
		this.notAfter = notAfter;
	}

	@Transient
	public String getNotAfter() {
		return notAfter;
	}

	public void setIssuedTo(String issuedTo) {
		this.issuedTo = issuedTo;
	}

	@Transient
	public String getIssuedTo() {
		return issuedTo;
	}

	public void setIssuedBy(String issuedBy) {
		this.issuedBy = issuedBy;
	}

	@Transient
	public String getIssuedBy() {
		return issuedBy;
	}

	public void setCertificateInfoId(long certificateInfoId) {
		this.certificateInfoId = certificateInfoId;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "certificate_info_id")
	public long getCertificateInfoId() {
		return certificateInfoId;
	}

	public void setCertificateDecoded(String certificateDecoded) throws CertificateException {
		this.certificateDecoded = certificateDecoded;
		Base64 base64Decoder = new Base64();
		byte[] certificateEncoded = base64Decoder.decode(certificateDecoded);
		ByteArrayInputStream inStream = new ByteArrayInputStream(certificateEncoded);
		CertificateFactory cf = CertificateFactory.getInstance("X.509");
		certificate = (X509Certificate) cf.generateCertificate(inStream);
		CertificateManager certificateManager = CertificateManager.getCertificateManager();
		this.issuedTo = certificateManager.getCertificateSubjectDNCN(certificate);
		this.issuedBy = certificateManager.getCertificateIssuerDNCN(certificate);
		SimpleDateFormat formatter = new SimpleDateFormat(Constants.SHORT_DATE_FORMAT);		
		this.notBefore =  formatter.format(certificate.getNotBefore());
		this.notAfter =  formatter.format(certificate.getNotAfter());
	}

	@Column(name = "certificate_decoded")
	@Lob
	public String getCertificateDecoded() {
		return certificateDecoded;
	}

}
